_pyname=rst2pdf
_realname=rst2pdf
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-rst2pdf")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.96
pkgrel=2
pkgdesc="Create PDFs from simple text markup, no LaTeX required (mingw-w64)"
arch=('any')
url='https://github.com/ralsina/rst2pdf'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-docutils"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-pdfrw"
         "${MINGW_PACKAGE_PREFIX}-python-pygments"
         "${MINGW_PACKAGE_PREFIX}-python-reportlab>=2.4"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-python-smartypants")
install=${_realname}-${CARCH}.install
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/${_realname}/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('b54fe6e6bd11211849e3f35c08954804378312005c328dd8185ea52e59829121')

prepare() {
  rm -rf python-build-${CARCH}| true
  cp -r "${_pyname}-${pkgver}" "python-build-${CARCH}"
  # master runs on python3, so this can be nuked in the near future
  ${MINGW_PREFIX}/bin/2to3 -w "${srcdir}/python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

install_doc_files() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      install -Dm644 $_fname "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/$_fname"

      rm -rf $_fname
    fi
  done
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install_doc_files CHANGES.rst \
    CODE_OF_CONDUCT.rst \
    Contributors.txt \
    MAINTAINERS.rst \
    ISSUE_TEMPLATE.md \
    README.rst 

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"


  # fix python command in files
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i rst2pdf-script.py
}
